<template>
  <section class="carousel-banner" v-if="slides.length > 0">
    <div class="carousel-container" ref="carouselContainer">
      <div class="carousel-wrapper" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
        <div
            v-for="(slide, index) in slides"
            :key="index"
            class="carousel-slide"
            @click="navigateTo(slide.link)"
        >
          <img :src="slide.image" :alt="slide.title" class="carousel-image">
          <div class="slide-content">
            <h2 class="slide-title">{{ slide.title }}</h2>
            <p class="slide-desc">{{ slide.description }}</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 指示器 -->
    <div class="carousel-indicators">
      <button
          v-for="(slide, index) in slides"
          :key="index"
          :class="{ 'active': index === currentIndex }"
          @click="goToSlide(index)"
      ></button>
    </div>

    <!-- 控制按钮 -->
    <button class="carousel-control prev" @click="prevSlide">
      <i class="fa fa-angle-left"></i>
    </button>
    <button class="carousel-control next" @click="nextSlide">
      <i class="fa fa-angle-right"></i>
    </button>
  </section>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
import { getBannerList } from '@/api/admin/banner';

const slides = ref([]);
const currentIndex = ref(0);
const carouselContainer = ref(null);
let intervalId = null;

// 请求后端轮播图数据
async function loadBanners() {
  const res = await getBannerList();
  if (res && res.data) {
    const banners = Array.isArray(res.data) ? res.data : (res.data.records || []);
    // 只显示isVisible为0的轮播图（1为不显示）
    slides.value = banners.filter(b => b.isVisible == 0).map(b => ({
      image: b.imageUrl,
      title: b.title,
      description: b.description || '',
      link: b.linkUrl
    }));
  }
}

// 自动轮播
const startAutoPlay = () => {
  intervalId = setInterval(() => {
    currentIndex.value = (currentIndex.value + 1) % slides.value.length;
  }, 5000);
};

// 停止自动轮播
const stopAutoPlay = () => {
  clearInterval(intervalId);
};

// 切换到上一张
const prevSlide = () => {
  currentIndex.value = (currentIndex.value - 1 + slides.value.length) % slides.value.length;
};

// 切换到下一张
const nextSlide = () => {
  currentIndex.value = (currentIndex.value + 1) % slides.value.length;
};

// 跳转到指定幻灯片
const goToSlide = (index) => {
  currentIndex.value = index;
};

// 导航到链接
const navigateTo = (link) => {
  window.location.href = link;
};

onMounted(() => {
  loadBanners();
  startAutoPlay();

  // 添加鼠标悬停暂停功能
  if (carouselContainer.value) {
    carouselContainer.value.addEventListener('mouseenter', stopAutoPlay);
    carouselContainer.value.addEventListener('mouseleave', startAutoPlay);
  }
});

onUnmounted(() => {
  stopAutoPlay();

  if (carouselContainer.value) {
    carouselContainer.value.removeEventListener('mouseenter', stopAutoPlay);
    carouselContainer.value.removeEventListener('mouseleave', startAutoPlay);
  }
});
</script>

<style scoped>
.carousel-banner {
  width: 100%;
  max-width: 100vw;
  height: 400px;
  position: relative;
  overflow: hidden;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.carousel-container {
  width: 100%;
  height: 100%;
  position: relative;
}

.carousel-wrapper {
  display: flex;
  height: 100%;
  transition: transform 0.5s ease-in-out;
}

.carousel-slide {
  min-width: 100%;
  height: 100%;
  position: relative;
  cursor: pointer;
}

.carousel-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.slide-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: #fff;
  z-index: 2;
  max-width: 800px;
  width: 100%;
  padding: 0 24px;
}

.slide-title {
  font-size: 3.5rem;
  font-weight: 800;
  letter-spacing: 1.5px;
  margin-bottom: 16px;
  text-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  animation: fadeInUp 0.8s ease-out;
}

.slide-desc {
  font-size: 1.6rem;
  font-weight: 400;
  opacity: 0.9;
  animation: fadeInUp 0.8s ease-out 0.2s;
}

/* 指示器样式 */
.carousel-indicators {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
  z-index: 3;
}

.carousel-indicators button {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.5);
  border: none;
  cursor: pointer;
  transition: all 0.3s;
}

.carousel-indicators button.active {
  background-color: #fff;
  width: 30px;
  border-radius: 6px;
}

/* 控制按钮样式 */
.carousel-control {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background-color: rgba(0, 0, 0, 0.3);
  color: #fff;
  border: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 3;
  transition: all 0.3s;
}

.carousel-control:hover {
  background-color: rgba(0, 0, 0, 0.6);
}

.prev {
  left: 20px;
}

.next {
  right: 20px;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>
